package com.qiu.greened.ui.main

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import androidx.paging.cachedIn
import com.qiu.greened.bean.ReplyBean
import com.qiu.greened.data.NotificationsReplyPagingSource
import com.qiu.greened.network.PostService
import com.qiu.greened.network.ServiceCreator
import com.qiu.greened.network.UserService
import com.qiu.greened.utils.MyData
import kotlinx.coroutines.flow.Flow

class NotificationsViewModel : ViewModel() {

    private val userService by lazy {
        ServiceCreator.create<UserService>()
    }

    /**
     * cachedIn() 将服务器返回的数据在 viewModelScope 这个作用域内进行缓存
     */
    fun getPagingData(): Flow<PagingData<ReplyBean>> {
        return Pager(PagingConfig(pageSize = 10)) {
            NotificationsReplyPagingSource(userService, MyData.userId)
        }.flow.cachedIn(viewModelScope)
    }
}